package com.magictower.primary.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.magictower.primary.R;
import com.magictower.primary.bean.AppInfo;
import com.magictower.primary.db.dao.AppLockDao;
import com.magictower.primary.engine.AppInfoParser;

import java.util.ArrayList;
import java.util.List;

/**
 * @author kejunzhan
 * @since 2017/12/20
 */

public class UnLockFragment extends Fragment {
    private TextView tvUnlock;
    private ListView listView;
    private List<AppInfo> unlockList;
    private AppLockDao dao;
    private UnLockAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.item_unlock_fragment, null);
        tvUnlock = (TextView) view.findViewById(R.id.tv_unlock);
        listView = (ListView) view.findViewById(R.id.list_view);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        //获取所有程序
        List<AppInfo> list = AppInfoParser.getAppInfos(getActivity());
        unlockList = new ArrayList<>();

        dao = new AppLockDao(getActivity());
        for (AppInfo appInfo : list) {
            if (dao.find(appInfo.getApkPackageName())) {
                //已锁定
            } else {
                //未锁定
                unlockList.add(appInfo);
            }
        }
        adapter = new UnLockAdapter();
        listView.setAdapter(adapter);
    }

    class UnLockAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            tvUnlock.setText("未加锁(" + unlockList.size() + ")个");
            return unlockList.size();
        }

        @Override
        public Object getItem(int position) {
            return unlockList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final View view;
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                view = View.inflate(getActivity(), R.layout.item_unlock, null);
                holder.iv_icon = (ImageView) view.findViewById(R.id.iv_icon);
                holder.tv_name = (TextView) view.findViewById(R.id.tv_name);
                holder.iv_unlock = (ImageView) view.findViewById(R.id.iv_unlock);
                view.setTag(holder);
            } else {
                view = convertView;
                holder = (ViewHolder) view.getTag();
            }

            final AppInfo app = unlockList.get(position);
            System.out.println("width:" + app.getIcon().getIntrinsicWidth() + "; height:" + app.getIcon().getIntrinsicHeight());
            System.out.println("minWidth:" + app.getIcon().getMinimumWidth() + "; minHeight:" + app.getIcon().getMinimumHeight());
            holder.iv_icon.setImageDrawable(app.getIcon());
            holder.tv_name.setText(app.getApkName());
            holder.iv_unlock.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    TranslateAnimation anim = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF, 0,
                            TranslateAnimation.RELATIVE_TO_SELF, 1.0F, TranslateAnimation.RELATIVE_TO_SELF, 0,
                            TranslateAnimation.RELATIVE_TO_SELF, 0);
                    anim.setDuration(3000);
                    view.startAnimation(anim);

                    new Thread() {
                        @Override
                        public void run() {
                            SystemClock.sleep(3000);
                            getActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    dao.add(app.getApkPackageName());
                                    unlockList.remove(app);
                                    adapter.notifyDataSetChanged();
                                }
                            });
                        }
                    }.start();
                }
            });
            return view;
        }
    }

    static class ViewHolder {
        ImageView iv_icon;
        TextView tv_name;
        ImageView iv_unlock;
    }

}
